package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/18
 * @description: 检查组业务实现类
 * @version:1.0
 */
@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao dao;
    @Transactional
    @Override
    public void add(CheckGroup checkGroup,Integer[] ids) {
        dao.addCheckGroup(checkGroup);
        for (Integer id : ids) {
            Map<String, Integer> map = new HashMap<>();
            map.put("checkGroupId",checkGroup.getId());
            map.put("checkItemId",id);
        dao.addCheckGroupAndCheckItem(map);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(),pageBean.getPageSize());
      Page<CheckGroup> page= dao.findAll(pageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer[] findCheckItemByCheckGroupId(Integer id) {
        return dao.findCheckItemByCheckGroupId(id);
    }

    @Override
    public CheckGroup findCheckGroupByCheckGroupId(Integer id) {
        return dao.findCheckGroupByCheckGroupId(id);
    }
    @Transactional
    @Override
    public void update(CheckGroup checkGroup, Integer[] ids) {
        dao.updateCheckGroup(checkGroup);
        dao.deleteCheckGroupAndCheckItem(checkGroup.getId());
        for (Integer id : ids) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroupId",checkGroup.getId());
            map.put("checkItemId",id);
            dao.addCheckGroupAndCheckItem(map);
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        return dao.findCheckGroup();
    }
}
